export var selectResult = ref();
export var insertResult = ref();
export var updateResult = ref();
export var deleteResult = ref();
export var saveResult = ref();
export const tableRef=ref(null);
 
//1查询数据源的变量常量
export var dataSourceTableConst = ref([]); 
export const selectData1 = async (jybh,obj,jylb) => {
    const res = await window.xinpuHttpApi(jybh,obj,jylb);
    dataSourceTableConst.value=res.ReContent;
    selectResult=res;
    console.log( dataSourceTableConst);
}

//1查询数据源的变量常量
export var dataSourceTableConst2 = ref([]); 
export const selectData2 = async (jybh,obj,jylb) => {
    const res = await window.xinpuHttpApi(jybh,obj,jylb);
    dataSourceTableConst2.value=res.ReContent.syfydl;
    selectResult=res;
    console.log( dataSourceTableConst2);
}

//1查询数据源的变量常量
export var dataSourceTableConstWeb1 = ref([]); 
export const selectDataWeb1 = async (jybh,obj) => {
    const res = await window.xinpuWebsApi(jybh, obj);
    dataSourceTableConstWeb1.value=res.ReContent;
    selectResult=res;
    console.log( dataSourceTableConstWeb1);
}

//2保存交易
let dataSourceTableListConst = {list:[]}  //临时数据源用来存储插入、修改、删除数据源 
export const saveData1=async(jybh,jylb)=>{
    let linshitableData=[];
    for(let i=0;i<dataSourceTableConst.value.length;i++){
      if(dataSourceTableConst.value[i].saveflag!=0){
         //移除这两个属性
        delete dataSourceTableConst.value[i].ReContent;
        delete dataSourceTableConst.value[i]._X_ROW_KEY;
        linshitableData.push(dataSourceTableConst.value[i]);  
      }
    }
    dataSourceTableListConst.list=linshitableData;
    const res = await window.xinpuHttpApi(jybh, dataSourceTableListConst,jylb);
    return res;
}
export const saveSave2=async(jybh,jylb,savejybh,selectobj)=>{
    let linshitableData=[];
    for(let i=0;i<dataSourceTableConst.value.length;i++){
      if(dataSourceTableConst.value[i].saveflag!=0){
         //移除这两个属性
        delete dataSourceTableConst.value[i].ReContent;
        delete dataSourceTableConst.value[i]._X_ROW_KEY;
        linshitableData.push(dataSourceTableConst.value[i]);  
      }
    }
    dataSourceTableListConst.list=linshitableData;
    const res = await window.xinpuHttpApi(jybh, dataSourceTableListConst,jylb);
    saveResult=res;  
    if(res.Flag==true){
        const res1 = await window.xinpuHttpApi(savejybh,selectobj,jylb);
        dataSourceTableConst.value=res.ReContent;
        selectResult=res1;
    }
}
//3.插入方法,mc是插入新行后把光标定位到新行的那个列的名称
export const insertFun=async (obj,mc)=>{
    //点插入的时候新建一行临时数据，数据里面给定一些默认值，其他让用户选择
    const insertRowObj=ref(obj);
    const $table = tableRef.value;
    dataSourceTableConst.value.push(insertRowObj.value);
    const { row: newRow } = await $table.insertAt(insertRowObj.value, -1);
    await $table.setEditCell(newRow, mc);

}


//4.修改方法
//2.4修改方法
//定义一个变量记录变化前后的值
let updateConst='';
export const updateFun=(row,a)=>{
    
  const $table = tableRef.value;
  if(a==1){
    updateConst=row.column.model.value;
  }
  else if(a==2){
    if(dataSourceTableConst.value[$table.getRowSeq(getPageRigthDataObj.value)-1].saveflag==0){
      if(updateConst!=row.column.model.value){
        
        dataSourceTableConst.value[$table.getRowSeq(row.row)-1].saveflag=2;
        //  判断成功后，就把updateConst恢复为空
        updateConst='';
      }
    }
  } 
  else if(a==3){
   
    cellClick(row);
  }
}

export const getPageRigthDataObj=ref({});
export const cellClickConst=ref('删除');
export const cellClick=(row)=>{
   
    if(row.row.saveflag==3){
      cellClickConst.value='取消删除';
    }
    else{
        cellClickConst.value='删除';
    }
    getPageRigthDataObj.value=row.row;
    watch(()=>[getPageRigthDataObj.value ], ([objnew ], [objold ]) => {
      // 右侧子界面的数据是来源于左侧表格点击后的一行对象的数据，要监听是在右侧修改这个行对象带来的改变，不
      // 要点击第一行后又点击第二行的数据带来的改变，所以这里要判断一下，上次的老数据必须和本次的新数据一直
      // 不然就认为你是点击了其他行带来的改变，所以就不做修改标记处理
      if(getPageRigthDataObj.value==objold){
        const $table = tableRef.value;
        //添加行后标记是3，要点到单元格做标记，这个位置和修改方法一致了，所以加个判断只有标记不是新增和删除类型的才做修改标记
        if(dataSourceTableConst.value[$table.getRowSeq(getPageRigthDataObj.value)-1]!=undefined){
            if(dataSourceTableConst.value[$table.getRowSeq(getPageRigthDataObj.value)-1].saveflag==0){
            dataSourceTableConst.value[$table.getRowSeq(getPageRigthDataObj.value)-1].saveflag=2;
          }
        }    
      }
   }, { deep: true });
}
//2.3删除方法
export const deleteFun=()=>{
    const $table = tableRef.value;
    const selectRecords = $table.getCurrentRecord();
    dataSourceTableConst.value[$table.getRowSeq(selectRecords)-1].saveflag=3;
    console.log(dataSourceTableConst.value[$table.getRowSeq(selectRecords)]);
}


export const biaoshi = (cellValue ) => {
  if(cellValue=='2')
  {
   return   '待改'
  }
  else if(cellValue=='1')
  {
   return  '待增'
  }
  else if(cellValue=='3')
  {
   return  '待删'
  }
  else if(cellValue=='9')
  {
   return  '待删'
  }
  else
  {
   return ''
  }  
}